package com.nandi.emergencyinspection.mvp

import android.util.Log
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.Callback
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.nandi.emergencyinspection.api.ApiServices
import com.nandi.emergencyinspection.bean.Bean
import com.tbright.ktbaselibrary.extension.response
import kotlinx.coroutines.launch
import okhttp3.*
import org.json.JSONObject
import java.io.IOException

/**
 * @创建者: qingsong
 * @创建时间: 2021/3/25 13:28
 * @描述:
 */
class LoginPresenter : LoginContract.LoginPresenter() {


    override fun login(
        token: String,
        code: String,
        randomStr: String,
        username: String,
        password: String
    ) {
        mainScope.launch {
            mView?.showLoading()
            val singlePoetry = ApiServices.instance.loginAsync(
                code = code,
                randomStr = randomStr,
                username = username,
                password = password
            ).response()
            if (singlePoetry != null) {
                mView?.loginResult(singlePoetry)
            }
            mView?.hideLoading()
        }


    }

    override fun getIm(name: String, id: String) {
        var map: MutableMap<String, String> = HashMap<String, String>()
        map["name"] = name
        map["id"] = id
        map["portrait"] = "http://www.rongcloud.cn/images/logo.png"
        mainScope.launch {
mView?.showLoading()
            val singlePoetry = ApiServices.instance.getImTokenAsync(map = map)
                .response()
            if (singlePoetry != null) {
                mView?.getIm(singlePoetry)
            }
            mView?.hideLoading()
        }

//        OkGo.post<String>("http://lhim.cn.utools.club/user/getToken")
//            .upJson(Gson().toJson(map))
//            .execute(object : StringCallback() {
//                override fun onSuccess(response: Response<String>) {
//                    try {
//                        val jsonObject = JSONObject(response.body().toString())
//                        val optString = jsonObject.optString("token")
//                        mView?.getIm(optString)
//                    }catch (e:Exception){
//                        Log.e("qs", "onError: ${e.message}")
//                    }
//
//                }
//
//                override fun onError(response: Response<String>?) {
//                    super.onError(response)
//                    Log.e("qs", "onError: ${response?.message()}", )
//                }
//
//            })
    }

    override fun getUser(d: String) {
        mainScope.launch {
            mView?.showLoading()
            val singlePoetry = ApiServices.instance.getUserInfoAsync(id = d)
                .response()
            if (singlePoetry != null) {
                mView?.getUserResult(singlePoetry)
            }
            mView?.hideLoading()
        }
    }

}